import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_demo/models/cacheConfig.dart';
import 'package:flutter_demo/models/profile.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'CacheObject.dart';
import 'Git.dart';


// 设置主题的常量
const _themes = <MaterialColor>[
  Colors.grey,
  Colors.blue,
  Colors.teal,
  Colors.yellow,
  Colors.red
];

class Global {

  static SharedPreferences _pref;

  static Profile pro = Profile();

  // 网络缓存对象
//  static NetCache cache = NetCache();

  static List<MaterialColor> get theme => _themes;

  // 是否是正式环境
  static bool get isRelease => bool.fromEnvironment("dart.vm.product");

  // 初始化全局变量，在app启动的时候执行
  static Future init() async {
    _pref = await SharedPreferences.getInstance();
    var _profile = _pref.get("profile");
    if (_profile != null) {
      try {
        pro = Profile.fromJson(jsonDecode(_profile));
      } catch(e) {
        print(e);
      }
    }

    // 给登陆信息里面换成付初始值
//    pro.cache = pro.cache ?? CacheConfig()
//      ..enable = true
//      ..maxAge = 3600
//      ..maxCount = 100;

    // 初始化网络
    Git.init();
  }


  // 将登录信息放入缓存中
  static setProfile() {
    _pref.setString("profile", jsonEncode(pro.toJson()));
  }
}